import {
    Controller,
    Get,
    Post,
    Ctx,
    Body,
    Params
} from 'koa-ts-controllers';
import {Context} from 'koa';
import {PostAddItemBody} from '../validators/shop';

import cubeStruct from '../define/cubeTypeSubtype';
import CUBE from '../utils/cubeData';
import myStatus from '../define/status';
import cubeQueue from '../utils/base/queue';

const items = new cubeQueue();

@Controller('/shop')
export class ShopController {


    @Post('/addItem')
    public async addItem(
        @Ctx() ctx: Context,
        @Body() body: PostAddItemBody
    ){

        let {itemName, itemID, itemDis, itemNum, itemValue} = body;
        let ret = {itemName, itemID, itemDis, itemValue, itemNum};
        console.log(itemName);
        let cubeIns = await CUBE.getinstance();

        let isSuccessful = await cubeIns.sendMessage(cubeStruct.eshop.role.type, cubeStruct.eshop.role.subtype.owner, body);
        if(!isSuccessful){
            return {
                code : myStatus.code.ERROR,
                data : '未能成功发送消息给cube'
            }
        }
        items.enqueue(ret);
        return {
            code : myStatus.code.SUCCESS,
            data : ret
        };
    }

    @Post('/delItem/:num')
    public async delItem(
        @Ctx() ctx: Context,
        @Params('num') num : number 
    ){
        let ret;
        for (let i = 0; i < num; i++) {
            ret = items.dequeue();
        }
         items.dequeue();
        if(!ret)
        {
            return {
                code : myStatus.code.ERROR,
            };
        }
        return {
            code : myStatus.code.SUCCESS,
            data : items.toString()
        };
    }

}